<?php

/**
 * +---------------------------------------------------------------
 * | Author: 北京永青宇创科技有限公司
 * | Date: 2020/03/12
 * | 版本: 6.0
 * +---------------------------------------------------------------
 * | CopyRight: 北京永青宇创科技有限公司 All rights reserved.
 * +---------------------------------------------------------------
 * | 版权声明：云豹不是一个自由软件，是云豹官方推出的商业源码。
 * | 严禁在未经许可的情况下拷贝、复制、传播、使用云豹的任意代码
 * | 如有违反，请立即删除，否则您将面临承担相应，法律责任的风险。
 * | 如果需要取得官方授权，请联系官方 https://www.gepardshop.com
 * +---------------------------------------------------------------
 */

namespace app\mall\controller\api;

use think\Controller;
use think\Db;
use think\controller\Rest;

class Goods extends Controller {
	protected $Model;

	public function initialize()
	{
		parent::initialize();
		config('default_return_type', 'json');
		$this->Model = model('mallGoods');
	}

	/**
	 * 积分兑换商品
	 */
	public function buy()
	{
		$id  = input('id', '0', 'intval');
		$num = input('num', '1', 'intval');

		$num = 1;

		if( ! \yunbao\user::is_login()) {
			$this->error('请先登录！');
		}
		//        $this->checkAuth('Shop/Index/goodsBuy', -1, '你没有购买、兑换商品的权限！');
		//
		//        $this->checkActionLimit('shop_goods_buy', 'shop', $id, \yunbao\user::is_login());

		$goods = $this->Model->where('id=' . $id)->find();

		if($goods) {
			if($num <= 0) {
				$this->error('商品购买数量不能为负数！');
			}
			//验证开始
			//判断商品余量
			if($num > $goods['num_stock']) {
				$this->error('商品库存不足！');
			}

			$money_need = $num * $goods['price'];


			//扣money

			$my_money = \yunbao\user::query_user('score');

			if($money_need > $my_money['score']) {
				$this->error('你的' . Modc('SCORE_TITLE', '', 'PAYCENTER') . '不足！');
			}

			//验证结束

			$data['goods_id']    = $id;
			$data['num_buy']     = $num;
			$data['price']       = $goods['price'];
			$data['status']      = 1;
			$data['user_id']         = \yunbao\user::is_login();
			$data['create_time'] = time();
			$data['money_total']       = $money_need;
			$data['out_trade_no']  = \yunbao\order::createOutTradeNo();
			$data['is_pay']  = 1;

			model('common/user')->where('id=' . \yunbao\user::is_login())->setDec('score', $goods['price']);
			//            $ScoreModel->setUserScore(array(\yunbao\user::is_login()), $money_need, $score_type, 'dec', 'shop', $id, get_nickname(\yunbao\user::is_login()) . '购买了商品');
			$res = model('mall/MallOrder')->allowField(true)->save($data);
			if($res) {
				//商品数量减少,已售量增加
				$this->Model->where('id=' . $id)->setDec('num_stock', $num);
				$this->Model->where('id=' . $id)->setInc('num_sold', $num);
				//发送系统消息
				// 构造消息数据
				$wexin_code          = substr($data['out_trade_no'], -4, 4);
				$msg_data['to_uid']  = \yunbao\user::is_login();
				$msg_data['title']   = '兑换成功';
				$msg_data['content'] = $goods['title'] . "购买成功，请等待发货。";


				messageLog($msg_data);
				//记录积分消费
				scoreLog(\yunbao\user::is_login(), -$money_need, get_nickname(\yunbao\user::is_login()) . '在' . time_format($data['create_time']) . '使用' . Modc('SCORE_TITLE', '', 'PAYCENTER') . '兑换了商品' . $goods['title']);

				//                actionLog('add_goods','shop_goods',$this->Model->id,\yunbao\user::is_login());
				actionLog('mall_goods_buy', 'mall_goods', $id, \yunbao\user::is_login());

				$this->success('兑换成功！花费了' . $money_need . Modc('SCORE_TITLE', '', 'PAYCENTER'),url('mall/my.order/index'));
			} else {
				return ['code' => 404, 'data' => $id, 'message' => '兑换失败！'];
			}
		} else {
			return ['code' => 404, 'data' => $id, 'message' => '请选择要购买的商品'];
		}
	}


}